<template>
  <div class="app-container">
    <div style="text-align:right;margin-bottom:5px;">
      <el-button type="primary" size="small" @click="goEdit(-1)">添加模板</el-button>
    </div>
    <el-row :gutter="20">
      <el-col v-for="(mould,k) in list" :key="k" :span="6">
        <el-card>
          <div>
            {{ mould.title }}
          </div>
          <div style="text-align:right">
            <el-button type="text" size="small" icon="el-icon-edit" @click="goEdit(k)">编辑</el-button>
            <el-button type="text" size="small" icon="el-icon-delete" @click="delShow(k)">删除</el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>
<script>
export default {
  data() {
    return {
      list: []
    }
  },
  mounted() {
    this.list = this.getMouldList()
  },
  methods: {
    getMouldList() {
      var list = []
      if (localStorage.getItem('mouldList')) {
        list = JSON.parse(localStorage.getItem('mouldList'))
      }
      return list
    },
    goEdit(i) {
      this.$router.push({ path: 'edit', query: { index: i }})
    },
    delShow(i) {
      const that = this
      var mouldName = this.list[i].title
      var m = `你确定要删除模板${mouldName}吗?`
      this.$msgbox({
        title: '消息',
        message: m,
        showCancelButton: true,
        confirmButtonText: '确定删除',
        cancelButtonText: '取消',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            instance.confirmButtonText = '删除中...'
            that.list.splice(i, 1)
            localStorage.setItem('mouldList', JSON.stringify(that.list))
            that.$message.success('删除成功')
            setTimeout(() => {
              done()
              instance.confirmButtonLoading = false
            }, 300)
          } else {
            done()
          }
        }
      }).then(action => {
      })
    }
  }
}
</script>
